package com.xzq.bustack;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.socket.SocketChannel;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @Author xzq
 * @Description //TODO
 * @Date 2021/11/19 14:34
 * @Version 1.0.0
 **/
public class MyServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        SocketChannel sc = (SocketChannel) ctx.channel();
        System.out.println("连接建立....");
        System.out.println("客户端IP: " + sc.localAddress().getHostString());
        System.out.println("客户端端口: " + sc.localAddress().getPort());
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " 接收到信息: " + msg);

        ctx.writeAndFlush("hi I am ok");
    }
}
